package cn.itsource.pojo.ro;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.constants.RConst;
import cn.itsource.domain.DriverAuthMaterial;
import lombok.Data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Data
public class SaveDriverAuthMaterialRo {
    private IDcard idcard;
    private Contact contact;
    private DrCard drcard;
    private Long driverId;

    public static DriverAuthMaterial toDriverAuthMaterial(SaveDriverAuthMaterialRo driverInfoRo){
        DriverAuthMaterial driverAuthMaterial = new DriverAuthMaterial();
        driverAuthMaterial.setId(Long.valueOf(driverInfoRo.getDriverId()));
        driverAuthMaterial.setDriverId(Long.valueOf(driverInfoRo.getDriverId()));
        driverAuthMaterial.setIdcardExpire(dateFormat(driverInfoRo.getIdcard().getIdcardExpire()));
        driverAuthMaterial.setDrcardExpire(dateFormat(driverInfoRo.getDrcard().getDrcardExpire()));
        driverAuthMaterial.setBirthday(dateFormat(driverInfoRo.getIdcard().getBirthday()));
        //设置审核状态
        driverAuthMaterial.setRealAuthStatus(RConst.Num.INT_0);
        driverAuthMaterial.setCreateTime(new Date());
        BeanUtil.copyProperties(driverInfoRo.getIdcard(),driverAuthMaterial);
        BeanUtil.copyProperties(driverInfoRo.getDrcard(),driverAuthMaterial);
        BeanUtil.copyProperties(driverInfoRo.getContact(),driverAuthMaterial);
        return driverAuthMaterial;
    }

    /**
     * 格式化日期
     * @param data
     * @return
     */
    private static Date dateFormat(String data) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(RConst.DataFormat.YYYY_MM_DD); // 定义日期格式
            Date date = formatter.parse(data); // 将字符串转换为日期
           return date;
        } catch (ParseException e) {
            e.printStackTrace(); // 如果解析过程中出现错误，打印错误堆栈
            throw new RuntimeException(e);
        }
    }

}
